package com.leoren.run.activity.run;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.leoren.run.R;
import com.leoren.run.activity.run.adapter.RunRecordAdapter;
import com.leoren.run.common.ResponseCode;
import com.leoren.run.common.ServerResponse;
import com.leoren.run.common.UrlConstants;
import com.leoren.run.dbbean.UserDB;
import com.leoren.run.log.L;
import com.leoren.run.utils.GsonUtil;
import com.leoren.run.utils.Http.OkHttp.CommonOkHttpClient;
import com.leoren.run.utils.Http.OkHttp.exception.OkHttpException;
import com.leoren.run.utils.Http.OkHttp.listener.DisposeDataHandle;
import com.leoren.run.utils.Http.OkHttp.listener.DisposeDataListener;
import com.leoren.run.utils.Http.OkHttp.request.CommonRequest;
import com.leoren.run.utils.ToastUtil;
import com.leoren.run.vo.User;
import com.leoren.run.vo.UserRunRecordVo;

import org.litepal.LitePal;

import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 展示用户跑步信息的界面
 */
public class RunRecordActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TextView allDistanceText;
    private ImageView headImg;
    private TextView countText;
    private TextView timeText;
    private TextView aveDistance;
    private RecyclerView recordListView;
    private TextView noDateText;

    private RunRecordAdapter adapter;
    private LinearLayoutManager manager;
    private UserRunRecordVo userRunRecordVo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_run_record);

        initLayout();

    }

    private void initLayout(){
        toolbar = findViewById(R.id.run_record_toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RunRecordActivity.this.finish();
            }
        });

        allDistanceText = findViewById(R.id.my_all_record_distance);
        headImg = findViewById(R.id.my_record_head);

        countText = findViewById(R.id.my_run_record_count);
        timeText = findViewById(R.id.my_run_time_);
        aveDistance = findViewById(R.id.my_run_ave_distance);

        recordListView = findViewById(R.id.my_record_list);
        noDateText = findViewById(R.id.no_record_data);

        initDate();
    }

    /**
     * 初始化信息
     */
    private void initDate(){
        final UserDB userDB = LitePal.findFirst(UserDB.class);
        RequestBody body = new FormBody.Builder()
                .add("userId", String.valueOf(userDB.getTagId()))
                .build();
        Request request = CommonRequest.createPostRequest(UrlConstants.GET_ALL_RECORD, body);
        CommonOkHttpClient.post(request, new DisposeDataHandle(new DisposeDataListener() {
            @Override
            public void onSuccess(String responseStr) {
                L.e(responseStr);
                ServerResponse<UserRunRecordVo> response = GsonUtil.formatJsonToUserRunRecordVo(responseStr);
                if(response != null && response.isSuccess()){
                    userRunRecordVo = response.getData();
                    initViewDate();
                    ToastUtil.MakeToast(response.getMsg());
                }else{
                    onFailure(new OkHttpException(ResponseCode.ERROR.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getValue()));
                }
            }

            @Override
            public void onFailure(OkHttpException e) {
                ToastUtil.MakeToast(e.getMsg().toString());
            }
        }));
    }

    /**
     * 显示地图列表信息
     */
    private void initViewDate(){
        allDistanceText.setText(userRunRecordVo.getAllDistance() + "");
        Glide.with(this).load(userRunRecordVo.getFaceImage()).into(headImg);
        countText.setText(userRunRecordVo.getRunCount() + "");
        timeText.setText(userRunRecordVo.getAllTimeHour() + "");
        aveDistance.setText(userRunRecordVo.getAveDistance() + "");

        if(userRunRecordVo.getRunRecordVos().size() <= 0){
            recordListView.setVisibility(View.GONE);
            noDateText.setVisibility(View.VISIBLE);
        }else {
            adapter = new RunRecordAdapter(this, userRunRecordVo.getRunRecordVos());
            manager = new LinearLayoutManager(this);
            recordListView.setLayoutManager(manager);
            recordListView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }

    }

}
